<?php
	/*
	* 商户后台常用函数
	*/
	
	/*
	* 验证用户是否登录
	*/
	function isLogin(){
		if($_SESSION['login']['userId']==''){
			Location("../index.php");
		}	
	}
	
	/*
	* 登录超时
	* @param	登录时间
	*/
	function user_mktime($loginTime){
		$newTime=time();
		$onlineTime=$newTime-$loginTime;//在线时间
		if($onlineTime>3600){
			$_SESSION['login']['userId']='';
			$_SESSION['login']['userName']='';
			$_SESSION['login']['companyname']='';
			$_SESSION['time']='';
			echo js('top.location.href="../index.php"');
		}else{
			$_SESSION['time']=time();	//更新SESSION中的时间
		}
	}
	
	/* 更新用户资料信息*/
	function updateInfo(){
		global $db;
		$data=$_POST['data'];
		if($db->updateRow('member_merchant',$data,'mem_id='.$_SESSION['login']['userId'])){
			Alert_Url('资料更新成功','user_info.php');
		}else{
			Alert_Url('资料更新失败','user_info.php');	
		}
	}
	
	/* 取得用户资料信息*/
	function getUserInfo(){
		global $db;
		$sql="select * from member_merchant where mem_id=".$_SESSION['login']['userId'];
		$rs=$db->GetOneSql($sql);
		return $rs;
	}
	/* 根据id取得用户名*/
	function getUserIName($id){
		global $db;
		$sql="select * from member_merchant where mem_id=$id";
		$rs=$db->GetOneSql($sql);
		return $rs;
	}
	
	/*修改密码*/
	function updatepassword(){
		global $db;
		$oldPwd=Post("oldpassword");
		$newPwd=Post("newpassword");
		$newPwd2=Post("anewpassword");
		$sql="select mem_pwd from member_merchant where mem_id=".$_SESSION['login']['userId'];
		//dump($sql);
		$rs=$db->GetOneSql($sql);
		if($rs['mem_pwd']!=md5($oldPwd)){
			Alert_Url('原来密码填写错误！','password.php');	
		}
		if($newPwd!=$newPwd2){
			Alert_Url('重复密码和新密码填写不一致','password.php');		
		}
		$sql="update member_merchant set mem_pwd='".md5($newPwd)."' where mem_id=".$_SESSION['login']['userId'];
		if($db->query($sql)){
			Alert_Url('密码更新成功','password.php');	
		}
		
	}
	/*账户充值更新*/
	function updateAccount(){
		global $db;
		$acc=getUserIName($_SESSION['login']['userId']);
		$data=$_POST['data'];
		$data['Fin_name']=$_SESSION['login']['userName'].'账户充值';
		$data['Fin_type']=0;
		$data['Fin_cst_id']=$_SESSION['login']['userId'];
		$data['Fin_createtime']=time();
		$data['Fin_amount_after_change']=$acc['mem_money'];
		if($db->InsertRow('fin_log',$data)){
			Alert_Url('充值记录提交成功，等待管理员审核','recharge_list.php');		
		}else{
			Alert_Url('充值记录提交失败，请联系管理员','recharge.php');		
		}
	}
	
	/*截取字符串*/
	function cutStr2($strInput,$intLength,$intStart=0,$strCode='utf-8'){
		$strInput=str_replace("&nbsp;"," ",$strInput);
		if(mb_strlen($strInput,$strCode)>$intLength){
			return(mb_substr($strInput,$intStart,$intLength,$strCode)."...");
		}else{
			return $strInput;
		}
	}
	
	/*列出2个日期之间的额日期*/
	function perDate($start,$end,$today){
		$str=array();
		//return $start.','.$end.','.$today;
		$tentime=strtotime(date('Y-m-d').'22:00:00');
		//dump($today.','.$tentime);
		if($today > $tentime){
			$today=	strtotime('+1 day',$today);
		}
		if($today < $end and $today >= $start){
			while ($today<=$end){
				$str[]=date('Y-m-d',$today);
				$today = strtotime('+1 day',$today);
			}
		}else if($today < $start){
			while ($start<=$end){
				$str[]=date('Y-m-d',$start);
				$today = strtotime('+1 day',$start);
			}	
		}
		return $str;
	}
	/*网站公告*/
	function getNotice(){
		global $db;
		$sql="select * from web_newslist where newstype_id=3 and isdel=0 order by news_creattime desc,news_updatetime desc limit 0,8";
		$rs=$db->GetAllSql($sql);
		return $rs;
	}
	function getNoticeDetail($id){
		global $db;
		$sql="select * from web_newslist where id=$id";
		$rs=$db->GetOneSql($sql);
		return $rs;
	}
	
	/*取得最新添加产品*/
	function getTopTicket(){
		global $db;
		$sql="select * from ticket order by Tkt_createtime desc limit 0,8";
		$rs=$db->GetAllSql($sql);
		return $rs;
	}
	
	function getImgFile($id){
		global $db;
		$sql="select * from attractions where Att_id=$id";
		$rs=$db->GetOneSql($sql);
		$img=$rs['Att_img'];
		return $img;
	}
?>